package com.vlin.api.mq.utils;

import org.apache.pulsar.client.api.PulsarClient;
import org.apache.pulsar.client.api.PulsarClientException;
import org.apache.pulsar.client.internal.DefaultImplementation;

import com.vlin.api.mq.config.MqAuthentication;
import com.vlin.api.mq.config.MqConfigs;

public class PulsarClientFactory {

	public static PulsarClient getPulsarClient() {
        return PulsarClientHolder.CLIENT;
    }

    public static class PulsarClientHolder {
        private static final PulsarClient CLIENT = getClient();

        private static PulsarClient getClient() {
            try {
                return DefaultImplementation.newClientBuilder().serviceUrl(MqConfigs.serviceUrl).allowTlsInsecureConnection(true)
                        .authentication(new MqAuthentication(MqConfigs.accessId, MqConfigs.accessKey)).build();
            } catch (PulsarClientException e) {
                System.err.println("PulsarClientFactory init error! e=" + e);
                return null;
            }
        }
    }
}
